<template>
  <span
    :type="$props.propsType||type" :size="$attrs['size']"
    v-if="selectOption&&selectOption.label">{{selectOption.label}}
  </span>
</template>
<script>
  import { ajaxDicList } from '@/utils/dic';

  export default {
    data() {
      return {
        innerValue: '',
        options: [],
      };
    },
    props: {
      code: { type: String, require: true },
      value: { type: [String, Number] },
      propsType: { type: [String, Number] },
    },
    mounted() {
      this.ajaxList();
    },
    computed: {


      type() {
        return this.labelForType(this.selectOption.label);
      },
      selectOption() {
        if (this.$data.options && this.$data.options.filter) {
          var one = this.$data.options.filter(item => item.value == this.$props.value);
          if (one && one[0]) {
            return one[0];
          }
        }
        return {};
      },
    },
    watch: {
      value(_val) {
        this.innerValue = _val;
      },
    },
    methods: {
      /**
       * label是这些的直接
       * @params str label
       * @return [success/info/warning/danger]
       * */
      labelForType(str) {
        var successList = ['处理中', '执行中', '已发起付款'];
        var warningList = [];
        var dangerList = ['停'];
        var primaryList = ['待开始', '待指派', '待裁', '待接单', '待报价', '待支付'];
        var infoList = ['结束', '待'];
        if (this.indexOfArray(successList, str)) {
          return 'success';
        }

        if (this.indexOfArray(warningList, str)) {
          return 'warning';
        }
        if (this.indexOfArray(dangerList, str)) {
          return 'danger';
        }
        if (this.indexOfArray(primaryList, str)) {
          return 'primary';
        }
        if (this.indexOfArray(infoList, str)) {
          return 'info';
        }
        return 'primary';
      },
      indexOfArray(arrays, str) {
        for (let i = 0; i < arrays.length; i++) {
          var __str = arrays[i];
          if (str.indexOf(__str) >= 0) {
            return true;
          }
        }
        return false;
      },

      changeValue(val) {
        this.innerValue = val;
        this.$emit('input', val);
      },
      ajaxList() {
        ajaxDicList && ajaxDicList(this.$props.code).then(list => {
          this.$data.options = list;
        }).catch(e => {
          console.error(`${this.$props.code}`,e);
        });
      },
    },
  };
</script>
